package com.syg.boot.util.number;

import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Locale;

/**
 * @author syg
 * @date 2021年11月24日19:43:12
 * @version 1.0
 */
public class NumberMoreUtil {

    public static byte[] toBytes(int value) {
        return Ints.toByteArray(value);
    }

    public static byte[] toBytes(long value) {
        return Longs.toByteArray(value);
    }

    public static byte[] toBytes(double val) {
        return toBytes(Double.doubleToRawLongBits(val));
    }

    public static int toInt(byte[] bytes) {
        return Ints.fromByteArray(bytes);
    }

    public static long toLong(byte[] bytes) {
        return Longs.fromByteArray(bytes);
    }

    public static double toDouble(byte[] bytes) {
        return Double.longBitsToDouble(toLong(bytes));
    }

    public static boolean isNumber(String str) {
        return NumberUtils.isNumber(str);
    }

    public static boolean isHexNumber(String value) {
        int index = value.startsWith("-") ? 1 : 0;
        return value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index);
    }

    public static int toInt(String str) {
        return NumberUtils.toInt(str, 0);
    }

    public static int toInt(String str, int defaultValue) {
        return NumberUtils.toInt(str, defaultValue);
    }

    public static long toLong(String str) {
        return NumberUtils.toLong(str, 0L);
    }

    public static long toLong(String str, long defaultValue) {
        return NumberUtils.toLong(str, defaultValue);
    }

    public static double toDouble(String str) {
        return NumberUtils.toDouble(str, 0.0D);
    }

    public static double toDouble(String str, double defaultValue) {
        return NumberUtils.toDouble(str, defaultValue);
    }

    public static Integer toIntObject(String str) {
        return toIntObject(str, (Integer)null);
    }

    public static Integer toIntObject(String str, Integer defaultValue) {
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        } else {
            try {
                return Integer.valueOf(str);
            } catch (NumberFormatException var3) {
                return defaultValue;
            }
        }
    }

    public static Long toLongObject(String str) {
        return toLongObject(str, (Long)null);
    }

    public static Long toLongObject(String str, Long defaultValue) {
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        } else {
            try {
                return Long.valueOf(str);
            } catch (NumberFormatException var3) {
                return defaultValue;
            }
        }
    }

    public static Double toDoubleObject(String str) {
        return toDoubleObject(str, (Double)null);
    }

    public static Double toDoubleObject(String str, Double defaultValue) {
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        } else {
            try {
                return Double.valueOf(str);
            } catch (NumberFormatException var3) {
                return defaultValue;
            }
        }
    }

    public static Integer hexToIntObject(String str) {
        return hexToIntObject(str, (Integer)null);
    }

    public static Integer hexToIntObject(String str, Integer defaultValue) {
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        } else {
            try {
                return Integer.decode(str);
            } catch (NumberFormatException var3) {
                return defaultValue;
            }
        }
    }

    public static Long hexToLongObject(String str) {
        return hexToLongObject(str, (Long)null);
    }

    public static Long hexToLongObject(String str, Long defaultValue) {
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        } else {
            try {
                return Long.decode(str);
            } catch (NumberFormatException var3) {
                return defaultValue;
            }
        }
    }

    public static String toString(int i) {
        return Integer.toString(i);
    }

    public static String toString(Integer i) {
        return i.toString();
    }

    public static String toString(long l) {
        return Long.toString(l);
    }

    public static String toString(Long l) {
        return l.toString();
    }

    public static String toString(double d) {
        return Double.toString(d);
    }

    public static String toString(Double d) {
        return d.toString();
    }

    public static String to2DigitString(double d) {
        return String.format(Locale.ROOT, "%.2f", d);
    }

    public static int toInt32(long x) throws IllegalArgumentException {
        if ((long)((int)x) == x) {
            return (int)x;
        } else {
            throw new IllegalArgumentException("Int " + x + " out of range");
        }
    }
}
